// components/searchView/searchView.js
var searchVal;
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    myKey:{
      type:String,
      value:"",
      observer(newVal){
        searchVal=newVal
        this.onClickSearch()
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    focusFlag:false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //输入框内容改变时
    onChange(e){
      searchVal = e.detail
    },
    //点击搜索
    onClickSearch(){
      wx.cloud.callFunction({
        name:"NE_search_get",
        data:{
          keyWord:searchVal
        }
      }).then(res=>{
        console.log("res",res);
        this.triggerEvent("history",searchVal)
        this.triggerEvent("mySearch",res.result.data)
      })
    },
    // 获取当前页面
    getRoute(){
      var pagePath = getCurrentPages();
      var curPage = pagePath[pagePath.length-1]
      var curUrl = curPage.route
      var arr = curUrl.split('/')
      var pageName = arr[arr.length-1]
      return pageName
    },


    // 获取焦点时搜索框跳转
    onFocus(){
      var name = this.getRoute()
      // console.log(name);
      if(name!="search"){
        wx.navigateTo({
          url: '/pages/search/search',
        })
      }
      
    }
  },

  lifetimes:{
    // 生命周期created
    attached(){
      searchVal = ""
      var name = this.getRoute()
      if(name=="search"){
        this.setData({
          focusFlag:true
        })
      }
    }
  }

  })